import java.util.Stack;
class Solution {
    public boolean isValid(String s) {
        if(s.length() % 2 == 0){
            Stack<Character> stack = new Stack();
            for(int i = 0 ; i < s.length() ; i++)
            {
                Character a = s.charAt(i);
                if(a.equals('(')){
                    stack.push(')');
                }
                else if(a.equals('[')){
                    stack.push(']');
                }
                else if(a.equals('{')){
                    stack.push('}');
                }
                else{
                    if(stack.isEmpty()){
                        return false;
                    }
                    Character b = stack.pop();
                    if(b.equals(a)){
                        continue;
                    }
                    else{
                        return false;
                    }
                }
            }
            return stack.isEmpty();
        }

        return false;
    }
}